<?php
/**
 * Created by PhpStorm.
 * User: lihui
 * Date: 3/26/2021
 * Time: 16:36
 */
namespace app\common\model\coin;

use app\common\model\base\A3Mall;
use app\common\model\file\Association;

use app\common\traits\recordAllRun;
use mall\basic\Users;
use think\facade\Db;

class Coin extends A3Mall
{
    use recordAllRun;   // 新增链表查询设置

    protected $name = "coin";

    /**
     * 关联文件类型
     */
    const FILE_TYPE = 'coin';

    protected $type = [
        "id"=>"integer",
        "code"=>"string",
        "platform"=>"integer",
        "name"=>"string",
        "create_time"=>"integer",
        "update_time"   => "integer",
        "status"=>"integer",
    ];

    const STATUS_OPEN = 1;    //开启
    const STATUS_CLOSE = 2; // 关闭

    //状态类型
    private static $status_text_mapping=[
        self::STATUS_OPEN=>'开启',
        self::STATUS_CLOSE=>'关闭'
    ];

    /**
     * 获取平台类型
     */
    public static function fetchPlatformList() {
        $list = [
            [
                'key'   => 1,
                'value' => '火币',
            ],
            [
                'key'   => 2,
                'value' => '币安',
            ],
            [
                'key'   => 3,
                'value' => 'okex',
            ],
            [
                'key'   => 4,
                'value' => '比特儿',
            ]
        ];
        return $list;
    }


    /**
     * 编辑货币
     * @param $details 任务详情
     * @param $updateData 需要编辑的数据
     */
    public function edit($id,$updateData)
    {
        if(is_array($updateData) && !empty($updateData)) {
            if(!$this->where([
                'id'    => $id,
                'status'    => static::STATUS_EFFECTIVE,
            ])->update($updateData)) {
                throw new \Exception('数据发生变化，请刷新重试');
            }
        }

        return true;
    }

    /**
     * 添加货币
     * @param $details 任务详情
     * @param $updateData 需要编辑的数据
     */
    public function add($data)
    {
        $data['update_time'] = time();
        if(!$this->save($data)){
            throw new \Exception('添加失败');
        }
    }

    /**
     * 获取货币详情
     * @param $id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findById($id,$userId = null) {
        $where = [
            'id'    => $id,
        ];
        if(!empty($userId)) {
            $where['publisher_id'] = $userId;
        }
        return $this->where($where)->find();
    }

    /**
     * 获取用户ID
     * @param null $userId 用户ID
     * @return int|mixed|null
     * @throws \Exception
     */
    private function getUserId($userId = null) {
        if(!$userId = is_integer($userId)?$userId:Users::get('id')) {
            throw new \Exception('获取用户ID信息失败');
        }
        return $userId;
    }

    //获取状态文本
    public static function getStatusText(int $status){
        return self::$status_text_mapping[$status] ?? '';
    }

    //获取状态列表
    public static function getStatusList()
    {
        return self::$status_text_mapping;
    }

    /**
     * 关联平台
     */
    public function coinPlatform() {
        return $this->hasOne(CoinPlatform::class,"id","platform")->joinType("INNER");
    }

    public function getList($condition,$size=10,$page=1){
        /*$count = $this->withJoin(["coinPlatform"])->where($condition)->count();
        $data = $this->withJoin(["coinPlatform"])->where($condition)->order('coin.id','DESC')->paginate($size);*/
        $count = $this->where($condition)->count();
        $data = $this->where($condition)->order('id','DESC')->paginate($size);
        return [
            "count"=>$count,
            "data"=>array_map([$this,'format'],$data->items())
        ];
    }

    public function getStatusTextAttr($value,$data)
    {
        return static::getStatusText($data['status']) ?? '';
    }


    public function getCreateTimeAttr($value){
        return date("Y-m-d H:i:s",$value);
    }

    public function getUpdateTimeAttr($value)
    {
       return date("Y-m-d H:i:s",$value);
    }

    protected function format($value)
    {
        $value->status_name=$value->status_text;
//        $value->type_name= $value->type_name ?? $value->job_type->name ?? $value->jobType__name;
//        $value->publisher=$value->username;
//        $value->publish_time_text=$value->publish_time_text;
//        $value->exp_time_text=$value->exp_time_text;
        return $value;
    }

}
